class Solution:
    def candy(self, ratings: list) -> int:
        candies = [1 for _ in range(len(ratings))]
        for i in range(1, len(ratings)):
            if ratings[i - 1] < ratings[i]:
                candies[i] = candies[i - 1] + 1
        for i in range(len(ratings) - 2, -1, -1):
            candy = candies[i + 1] + 1 if ratings[i] > ratings[i + 1] else 1
            candies[i] = max(candy, candies[i])

        return sum(candies)
